/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.elcom.emocbc.dbmapping.persistence;

import com.elcom.emocbc.dbmapping.tables.daos.SmscbMessageResultDao;
import com.elcom.emocbc.dbmapping.tables.pojos.SmscbMessageResult;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.List;
import org.jooq.Configuration;
import org.jooq.ConnectionProvider;
import org.jooq.DSLContext;
import org.jooq.SQLDialect;
import org.jooq.conf.Settings;
import org.jooq.conf.StatementType;
import org.jooq.impl.DSL;
import org.jooq.impl.DefaultConfiguration;

/**
 *
 * @author GiangLT
 */
public class SmscbMessageResultPersistance extends SmscbMessageResultDao {

    public SmscbMessageResultPersistance() throws SQLException {
        ConnectionProvider connectionProvider = new DbConnectionProvider();
        Settings settings = new Settings();
        settings.setStatementType(StatementType.STATIC_STATEMENT);
        Configuration configuration = new DefaultConfiguration()
                .set(connectionProvider)
                .set(SQLDialect.ORACLE)
                .set(settings);
        this.setConfiguration(configuration);
    }
    private final com.elcom.emocbc.dbmapping.tables.SmscbMessageResult SMSCB_MESSAGE_RESULT = com.elcom.emocbc.dbmapping.tables.SmscbMessageResult.SMSCB_MESSAGE_RESULT;

    public int update(String msgSeqId, Timestamp startTime, Timestamp endTime, String state) throws SQLException {
        Connection con = DbAccess.getInstance().getConnection();
        try {
            DSLContext context = DSL.using(con, SQLDialect.ORACLE);
            return context.update(SMSCB_MESSAGE_RESULT)
                    .set(SMSCB_MESSAGE_RESULT.BROADCAST_STATE, state)
                    .set(SMSCB_MESSAGE_RESULT.START_TIME, startTime)
                    .set(SMSCB_MESSAGE_RESULT.END_TIME, endTime)
                    .where(SMSCB_MESSAGE_RESULT.MSG_SEQ_ID.equal(msgSeqId))
                    .execute();
        } finally {
            DbAccess.commitTransaction(con);
            DbAccess.closeConnection(con);
        }
    }
    
    public List<SmscbMessageResult> findInBroadcastTimeRange(Timestamp startTime, Timestamp endTime) throws SQLException {
        Connection con = DbAccess.getInstance().getConnection();
        try {
            DSLContext context = DSL.using(con, SQLDialect.ORACLE);
            return context.selectFrom(SMSCB_MESSAGE_RESULT)
                    .where(SMSCB_MESSAGE_RESULT.SUMMITTED_TIME.between(startTime, endTime))
                    .fetchInto(SmscbMessageResult.class);
        } finally {
            DbAccess.closeConnection(con);
        }
    }
}
